package marketing

import (
	"encoding/json"
	"jlpay-sdk-go/pkg/jlpay/core"
	"jlpay-sdk-go/pkg/jlpay/models"
	"log"
)

// MarketingActivityQueryRequest 营销活动查询请求
type MarketingActivityQueryRequest struct {
	models.OrgBaseRequest
	// SponsorID 赞助商ID
	SponsorID string `json:"sponsor_id,omitempty"`
	// Page 页码
	Page string `json:"page,omitempty"`
	// Limit 每页数量限制
	Limit string `json:"limit,omitempty"`
}

// NewMarketingActivityQueryRequest 创建新的营销活动查询请求
func NewMarketingActivityQueryRequest() *MarketingActivityQueryRequest {
	return &MarketingActivityQueryRequest{}
}

// SetSponsorID 设置赞助商ID
func (m *MarketingActivityQueryRequest) SetSponsorID(sponsorID string) *MarketingActivityQueryRequest {
	m.SponsorID = sponsorID
	return m
}

// SetPage 设置页码
func (m *MarketingActivityQueryRequest) SetPage(page string) *MarketingActivityQueryRequest {
	m.Page = page
	return m
}

// SetLimit 设置每页数量限制
func (m *MarketingActivityQueryRequest) SetLimit(limit string) *MarketingActivityQueryRequest {
	m.Limit = limit
	return m
}

// CheckRequiredParams 检查必需参数
func (m *MarketingActivityQueryRequest) CheckRequiredParams(config *core.Config) error {
	// 营销活动查询接口不需要额外的必需参数校验
	// 基础参数（如app_id等）由OrgBaseRequest处理
	return nil
}

// Path 获取接口路径
func (m *MarketingActivityQueryRequest) Path() string {
	return "/open/org/bank/act-bridge/act/list/sponsor"
}

// ToMap 将请求对象转换为map，过滤掉值为空的字段
func (m *MarketingActivityQueryRequest) ToMap() (map[string]interface{}, error) {
	data, err := m.OrgBaseRequest.ToMap()
	if err != nil {
		return nil, err
	}

	// 添加当前字段
	if m.SponsorID != "" {
		data["sponsor_id"] = m.SponsorID
	}
	if m.Page != "" {
		data["page"] = m.Page
	}
	if m.Limit != "" {
		data["limit"] = m.Limit
	}

	return data, nil
}

// MarketingActivityData 营销活动数据
type MarketingActivityData struct {
	// ActID 活动ID
	ActID string `json:"act_id,omitempty"`
	// ActName 活动名称
	ActName string `json:"act_name,omitempty"`
	// SponsorID 赞助商ID
	SponsorID string `json:"sponsor_id,omitempty"`
	// SponsorName 赞助商名称
	SponsorName string `json:"sponsor_name,omitempty"`
	// StartTime 活动开始时间
	StartTime string `json:"start_time,omitempty"`
	// EndTime 活动结束时间
	EndTime string `json:"end_time,omitempty"`
	// Status 活动状态
	Status string `json:"status,omitempty"`
	// ActType 活动类型
	ActType string `json:"act_type,omitempty"`
	// CreateTime 创建时间
	CreateTime string `json:"create_time,omitempty"`
	// UpdateTime 更新时间
	UpdateTime string `json:"update_time,omitempty"`
}

// NewMarketingActivityData 创建新的营销活动数据
func NewMarketingActivityData() *MarketingActivityData {
	return &MarketingActivityData{}
}

// SetActID 设置活动ID
func (m *MarketingActivityData) SetActID(actID string) *MarketingActivityData {
	m.ActID = actID
	return m
}

// SetActName 设置活动名称
func (m *MarketingActivityData) SetActName(actName string) *MarketingActivityData {
	m.ActName = actName
	return m
}

// SetSponsorID 设置赞助商ID
func (m *MarketingActivityData) SetSponsorID(sponsorID string) *MarketingActivityData {
	m.SponsorID = sponsorID
	return m
}

// SetSponsorName 设置赞助商名称
func (m *MarketingActivityData) SetSponsorName(sponsorName string) *MarketingActivityData {
	m.SponsorName = sponsorName
	return m
}

// SetStartTime 设置活动开始时间
func (m *MarketingActivityData) SetStartTime(startTime string) *MarketingActivityData {
	m.StartTime = startTime
	return m
}

// SetEndTime 设置活动结束时间
func (m *MarketingActivityData) SetEndTime(endTime string) *MarketingActivityData {
	m.EndTime = endTime
	return m
}

// SetStatus 设置活动状态
func (m *MarketingActivityData) SetStatus(status string) *MarketingActivityData {
	m.Status = status
	return m
}

// SetActType 设置活动类型
func (m *MarketingActivityData) SetActType(actType string) *MarketingActivityData {
	m.ActType = actType
	return m
}

// SetCreateTime 设置创建时间
func (m *MarketingActivityData) SetCreateTime(createTime string) *MarketingActivityData {
	m.CreateTime = createTime
	return m
}

// SetUpdateTime 设置更新时间
func (m *MarketingActivityData) SetUpdateTime(updateTime string) *MarketingActivityData {
	m.UpdateTime = updateTime
	return m
}

// ToMap 将对象转换为map
func (m *MarketingActivityData) ToMap() (map[string]interface{}, error) {
	data := make(map[string]interface{})

	if m.ActID != "" {
		data["act_id"] = m.ActID
	}
	if m.ActName != "" {
		data["act_name"] = m.ActName
	}
	if m.SponsorID != "" {
		data["sponsor_id"] = m.SponsorID
	}
	if m.SponsorName != "" {
		data["sponsor_name"] = m.SponsorName
	}
	if m.StartTime != "" {
		data["start_time"] = m.StartTime
	}
	if m.EndTime != "" {
		data["end_time"] = m.EndTime
	}
	if m.Status != "" {
		data["status"] = m.Status
	}
	if m.ActType != "" {
		data["act_type"] = m.ActType
	}
	if m.CreateTime != "" {
		data["create_time"] = m.CreateTime
	}
	if m.UpdateTime != "" {
		data["update_time"] = m.UpdateTime
	}

	return data, nil
}

// MarketingActivityQueryResponse 营销活动查询响应
type MarketingActivityQueryResponse struct {
	models.StandardResponse
	// Data 活动数据列表
	Data []*MarketingActivityData `json:"data,omitempty"`
}

// NewMarketingActivityQueryResponse 创建营销活动查询响应
func NewMarketingActivityQueryResponse() *MarketingActivityQueryResponse {
	return &MarketingActivityQueryResponse{}
}

// HandleResponse 处理响应并打印结果
func (m *MarketingActivityQueryResponse) HandleResponse(operationName string) {
	// 直接打印完整的响应报文
	originData := m.GetOriginData()
	if originData != nil {
		responseJSON, _ := json.MarshalIndent(originData, "", "  ")
		log.Printf("响应报文：\n%s", string(responseJSON))
	}

	// 打印基本状态信息
	if m.IsSuccess() {
		log.Printf("%s成功！", operationName)
	} else {
		log.Printf("%s失败: %s - %s", operationName, m.GetRetCode(), m.GetRetMsg())
	}
}
